//
//  SCAN&BUY.m
//  GUIinterface
//
//  Created by Anurak on 1/29/55 BE.
//  Copyright (c) 2555 __MyCompanyName__. All rights reserved.
//

#import "SCAN&BUY.h"
#import "SCAN_ITEM.h"

#import "AppDelegate.h"
#import "ScannedImgAppDataObject.h"
#import "AppDelegateProtocol.h"

@implementation SCAN_BUY

@synthesize resultImage, resultText, okButton;

#pragma mark -
#pragma mark instance methods

- (ScannedImgAppDataObject*) theAppDataObject;
{
	id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
	ScannedImgAppDataObject* theDataObject;
	theDataObject = (ScannedImgAppDataObject*) theDelegate.theAppDataObject;
	return theDataObject;
}




- (IBAction)scanButtonTapped
{
    NSLog(@"TBD: scan barcode here...");
    // create a barcode reader
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
        
    reader.readerDelegate = self;
    
    reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    
    // get the scanner
    ZBarImageScanner *scanner = reader.scanner;
    
    // config the scanner: disable I2/5 to improve performance
    [scanner setSymbology: ZBAR_I25 
             config: ZBAR_CFG_ENABLE 
             to: 0];
    
    [self presentModalViewController: reader 
          animated: YES];
    
    //[reader release]; // not applicable with ARC!!
    
}

// handle the result
#pragma mark ZBar delegate
- (void) imagePickerController: (UIImagePickerController*)reader
 didFinishPickingMediaWithInfo: (NSDictionary*)info
{
    id<NSFastEnumeration> results = 
        [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for (symbol in results)
        // just grab the first barcode
        break;

	ScannedImgAppDataObject* theDataObject = [self theAppDataObject];
	theDataObject.itemID = symbol.data;    
    theDataObject.qrCodeImg = 
        [info objectForKey: UIImagePickerControllerOriginalImage];
        

    resultText.text = symbol.data;
    resultImage.image = [info objectForKey: UIImagePickerControllerOriginalImage];
    okButton.hidden = NO;
    
    // Product Image
    // Product Info
    
    [reader dismissModalViewControllerAnimated: YES];

//    [self performSegueWithIdentifier:@"scannedSegue" sender:(id)@"J3S-hA-HIS"];
    
    
}


- (void) dealloc
{
    self.resultText = nil;
    self.resultImage = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    //return (YES);
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



@end
